﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;

namespace Besture.CmsControl
{
    public class DataModel : WebControl
    {
        private string _ItemID;
        /// <summary>
        /// 编号
        /// </summary>
        public string ItemId
        {
            get { return _ItemID; }
            set { _ItemID = value; }
        }
        /// <summary>
        /// 数据行，用于供DataField调用
        /// </summary>
        public DataRow Row { get; set; }

        /// <summary>
        /// 错误提示
        /// </summary>
        public string ErrorText { get; set; }

        public string _DataTableName;
        [Description("数据表名称")]
        public string DataTableName
        {
            set
            {
                _DataTableName = value;
                var request = System.Web.HttpContext.Current.Request;
                if (request.QueryString[_ItemID] != null)
                {
                    try
                    {
                        string sql = string.Format(@"SELECT * FROM {0} WHERE {1} = {2}", _DataTableName, _ItemID, request.QueryString[_ItemID].ToString());
                        DataTable table = QueryDac.GetTable(sql);
                        //只取一条数据
                        if (table.Rows.Count > 0)
                        {
                            Row = table.Rows[0];
                        }
                    }
                    catch (Exception e)
                    {
                        ErrorText = "查询数据出现错误，请检查！";
                    }
                }
                else
                {
                    ErrorText = "未查询到相关参数，请检查！";
                }
            }
        }

        

        public string ModelName
        {
            set
            {
            {
                var request = System.Web.HttpContext.Current.Request;
                if (request.QueryString[_ItemID] != null)
                {
                    try
                    {

                        string sqlTable = string.Format(@"SELECT TableName FROM TBContentModel WHERE NAME = '{0}'",value);
                        string tableName = string.Empty;
                        DataTable table1 = QueryDac.GetTable(sqlTable);
                        if(table1.Rows.Count>0)
                        {
                            tableName = table1.Rows[0][0].ToString();
                        }
                        string sql = string.Format(@"SELECT * FROM {0} WHERE {1} = {2}", tableName, _ItemID, request.QueryString[_ItemID].ToString());
                        DataTable table = QueryDac.GetTable(sql);
                        //只取一条数据
                        if (table.Rows.Count > 0)
                        {
                            Row = table.Rows[0];
                        }
                    }
                    catch (Exception e)
                    {
                        ErrorText = "查询数据出现错误，请检查！";
                    }
                }
                else
                {
                    ErrorText = "未查询到相关参数，请检查！";
                }
            }
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(ErrorText);
        }
    }
}
